﻿using System;
using System.Collections.Generic;
using System.Configuration;
using E4RPTools;

namespace DBTractores
{
    public class Controller
    {
        #region Members

        private readonly int _hashcode;
        private readonly DataClassesDataContext _dataClasses = new DataClassesDataContext();
        
        #endregion

        #region Events

        public event EventHandler DocumentProcessing;

        #endregion

        #region Properties

        public PartReader PartReader { get; private set; }

        public Dictionary<string, Part> Parts { get; private set; }

        public List<Part> ExistingParts { get; private set; }

        public PartMerger PartMerger { get; private set; }

        public PartUpdater PartUpdater { get; private set; }

        public Result Errors { get; private set; }

        public bool ValidLicence
        {
            get
            {
                var timeBomb = new DateTime(2008, 12, 1);
                return _hashcode.ToString() == ConfigurationManager.AppSettings["licence"] || (ConfigurationManager.AppSettings["licence"] == "demo" && DateTime.Now < timeBomb);
            }
        }

        #endregion

        #region Public Methods

        public Controller()
        {
            var company = ConfigurationManager.AppSettings["company"];
            var user = ConfigurationManager.AppSettings["user"];
            _hashcode = (company + user).GetHashCode();
            Errors = new Result();
            Parts = new Dictionary<string, Part>();
            ExistingParts = new List<Part>();
            PartReader = new PartReader(Errors, Parts);
            PartMerger = new PartMerger(Errors, Parts, _dataClasses, ExistingParts);
            PartUpdater = new PartUpdater(Errors, _dataClasses, ExistingParts);
        }

        #endregion
    }
}

